using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using GoogleEarthClasses.Global;
namespace GoogleEarthClasses.Data
{
    public class DImage
    {
        #region Query
        public static DataTable SelectPhotosByPosition(Int32 IdPosition)
        {
            SqlDataAdapter DataAdapter = null;
            try
            {
                DataAdapter = new SqlDataAdapter("sp_Images_SelectByPosition", DBHelper.GetConnection());
                DataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataAdapter.SelectCommand.Parameters.Add(DBHelper.CreateIntParameter("@IdPosition", ParameterDirection.Input, false, IdPosition));
                DataTable _DataTable = new DataTable();
                DataAdapter.Fill(_DataTable);
                return _DataTable;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (DataAdapter.SelectCommand != null) DataAdapter.SelectCommand.Connection.Close();
            }
        }
         
        #endregion

        public static void DeleteIncompleted(Int32 IdPosition)
        {
            SqlCommand cmd = null;
            SqlTransaction tr = null;
            try
            {
                cmd = new SqlCommand("sp_Images_DeleteIncompleted", DBHelper.GetConnection());
                tr = cmd.Connection.BeginTransaction();
                cmd.Transaction = tr;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(DBHelper.CreateIntParameter("@IdPosition", ParameterDirection.Input, false, IdPosition));
                cmd.ExecuteNonQuery();
                tr.Commit();
            }
            catch (SqlException sex)
            {
                if (tr != null)
                    tr.Rollback();
            }
            catch (Exception ex)
            {
                if (tr != null)
                    tr.Rollback();
            }
            finally
            {
                if (cmd != null) cmd.Connection.Close();
            }
            
        }

        public static byte[] GetImage(int ImageId)
        {
            SqlDataAdapter DataAdapter = null;
            try
            {
                DataAdapter = new SqlDataAdapter("sp_Image_SelectBytes", DBHelper.GetConnection());
                DataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataAdapter.SelectCommand.Parameters.Add(DBHelper.CreateIntParameter("@IdImage", ParameterDirection.Input, false, ImageId));
                return (byte[])DataAdapter.SelectCommand.ExecuteScalar();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (DataAdapter.SelectCommand != null) DataAdapter.SelectCommand.Connection.Close();
            }
        }
    }
}
